package com.action.wx.cash;

import com.action.pc.rebate.RebateVo;
import com.bean.*;
import com.common.BaseAction;
import com.common.ModelAndJson;
import com.common.SystemConfig;
import com.interceptor.CheckWxLogin;
import com.service.bank.BankCardService;
import com.service.cash.CashService;
import com.service.member.MbAccountService;
import com.service.member.MemberService;
import com.util.Config;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by lvjianqing on 2017/9/28.
 * 余额、佣金提现
 */
@Controller
@RequestMapping("wx/cash")
public class CashApplyAction extends BaseAction {
    @Autowired
    private MbAccountService mbAccountService;
    @Autowired
    private BankCardService bankCardService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private CashService cashService;

    /***************************************************************佣金提现************************************************************/
    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("rebate")
    public ModelAndView rebateCash(ModelMap modelMap) {
        HttpSession session = this.getSession();
        Integer mid = (Integer) session.getAttribute(SystemConfig.MemberIdSession);
        Mb_account account = mbAccountService.selectByMemberId(mid);
        modelMap.put("account", account);
        Float prop = StringUtil.String2Float(Config.getString("cash_discount", "0.03"));
        modelMap.put("prop", prop * 100);
        List<Bank_card> list = bankCardService.getBankCardList(mid);
        modelMap.put("bclist", list);
        return new ModelAndView("/wx/rebate/rebate_cash", modelMap);
    }

    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("rebateAlipay")
    public ModelAndView rebateAlipay(ModelMap modelMap, CashApplyVo vo) {
        BigDecimal amount = vo.getAmount();
        if (amount != null) {
            modelMap.put("amount", amount);
        }
        return new ModelAndView("/wx/rebate/rebate_cash_alipay", modelMap);
    }

    /**
     * 佣金提现
     *
     * @param request
     * @param model
     * @param vo
     * @return
     */
    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("cashApplyRebate")
    @ResponseBody
    public String cashApplyRebate(HttpServletRequest request, ModelAndJson model, CashApplyVo vo) {
        HttpSession session = this.getSession();
        Integer mid = (Integer) session.getAttribute(SystemConfig.MemberIdSession);
        Member member = memberService.selectByPrimaryKey(mid);
        Integer cid = member.getCityid();
        if (mid != null && cid != null) {
            Mb_account account = mbAccountService.selectByMemberId(mid);
            if (account != null) {
                BigDecimal amount = vo.getAmount();
                BigDecimal rebate = account.getRebate();
                String realname = vo.getRealname();
                double x = rebate.subtract(amount).doubleValue();
                if (x >= 0) {
                    Byte paytype = vo.getPaytype();
                    Cash_apply cash = new Cash_apply();
                    cash.setAmount(amount);
                    cash.setCreaettime(new Date());
                    cash.setPaytype(paytype);
                    cash.setRealname(realname);
                    cash.setMemberid(mid);
                    cash.setCityid(cid);
                    cash.setCategory((byte) 1);
                    if (paytype != null) {//支付宝
                        if (paytype == 1) {
                            String aliaccount = vo.getAliaccount();
                            if (StringUtil.isNotEmpty(aliaccount)) {
                                cash.setAliaccount(aliaccount);
                                try {
                                    cashService.saveCashRebate(cash, account);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    model.setFalse("-1", e.getMessage());
                                }
                            } else {
                                model.setFalse("-1", "支付宝账号不存在");
                            }
                        } else if (paytype == 2) {
                            String bankname = vo.getBankname();
                            String bankaccount = vo.getBankaccount();
                            String bankplace = vo.getBankplace();
                            if (StringUtil.isNotEmpty(bankname) && StringUtil.isNotEmpty(bankaccount) && StringUtil.isNotEmpty(bankplace)) {
                                cash.setBankname(bankname);
                                cash.setBankaccount(bankaccount);
                                cash.setBankplace(bankplace);
                                try {
                                    cashService.saveCashRebate(cash, account);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    model.setFalse("-1", e.getMessage());
                                }
                            } else {
                                model.setFalse("-1", "银行信息不全");
                            }
                        }
                    } else {//银行卡
                        model.setFalse("-1", "非法操作");
                    }
                } else {
                    model.setFalse("-1", "提现金额大于账户余额");
                }
            } else {
                model.setFalse("-1", "账户不存在");
            }
        } else {
            model.setFalse("-1", "请登录后操作");
        }
        return model.toString();
    }

    /***************************************************************余额提现************************************************************/
    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("balance")
    public ModelAndView balanceCash(ModelMap modelMap) {
        HttpSession session = this.getSession();
        Integer mid = (Integer) session.getAttribute(SystemConfig.MemberIdSession);
        Mb_account account = mbAccountService.selectByMemberId(mid);
        modelMap.put("account", account);
        Float prop = StringUtil.String2Float(Config.getString("cash_discount", "0.03"));
        modelMap.put("prop", prop * 100);
        List<Bank_card> list = bankCardService.getBankCardList(mid);
        modelMap.put("bclist", list);
        return new ModelAndView("/wx/balance/balance_cash", modelMap);
    }

    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("balanceAlipay")
    public ModelAndView balanceAlipay(ModelMap modelMap, CashApplyVo vo) {
        BigDecimal amount = vo.getAmount();
        if (amount != null) {
            modelMap.put("amount", amount);
        }
        return new ModelAndView("/wx/balance/balance_cash_alipay", modelMap);
    }

    /**
     * 佣金提现
     *
     * @param request
     * @param model
     * @param vo
     * @return
     */
    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("cashApplyBalance")
    @ResponseBody
    public String cashApplyBalance(HttpServletRequest request, ModelAndJson model, CashApplyVo vo) {
        HttpSession session = this.getSession();
        Integer mid = (Integer) session.getAttribute(SystemConfig.MemberIdSession);
        Member member = memberService.selectByPrimaryKey(mid);
        Integer cid = member.getCityid();
        if (mid != null && cid != null) {
            Mb_account account = mbAccountService.selectByMemberId(mid);
            if (account != null) {
                BigDecimal amount = vo.getAmount();
                BigDecimal balance = account.getBalance();
                String realname = vo.getRealname();
                double x = balance.subtract(amount).doubleValue();
                if (x >= 0) {
                    Byte paytype = vo.getPaytype();
                    Cash_apply cash = new Cash_apply();
                    cash.setAmount(amount);
                    cash.setCreaettime(new Date());
                    cash.setPaytype(paytype);
                    cash.setRealname(realname);
                    cash.setMemberid(mid);
                    cash.setCityid(cid);
                    cash.setCategory((byte) 2);
                    if (paytype != null) {//支付宝
                        if (paytype == 1) {
                            String aliaccount = vo.getAliaccount();
                            if (StringUtil.isNotEmpty(aliaccount)) {
                                cash.setAliaccount(aliaccount);
                                try {
                                    cashService.saveCashBalance(cash, account);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    model.setFalse("-1", e.getMessage());
                                }
                            } else {
                                model.setFalse("-1", "支付宝账号不存在");
                            }
                        } else if (paytype == 2) {
                            String bankname = vo.getBankname();
                            String bankaccount = vo.getBankaccount();
                            String bankplace = vo.getBankplace();
                            if (StringUtil.isNotEmpty(bankname) && StringUtil.isNotEmpty(bankaccount) && StringUtil.isNotEmpty(bankplace)) {
                                cash.setBankname(bankname);
                                cash.setBankaccount(bankaccount);
                                cash.setBankplace(bankplace);
                                try {
                                    cashService.saveCashBalance(cash, account);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    model.setFalse("-1", e.getMessage());
                                }
                            } else {
                                model.setFalse("-1", "银行信息不全");
                            }
                        }
                    } else {//银行卡
                        model.setFalse("-1", "非法操作");
                    }
                } else {
                    model.setFalse("-1", "提现金额大于账户余额");
                }
            } else {
                model.setFalse("-1", "账户不存在");
            }
        } else {
            model.setFalse("-1", "请登录后操作");
        }
        return model.toString();
    }

}
